layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
 
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/shop/activity/activitygoods.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
            //封装返回数据
			console.log(res);
			for (i in res.data) {
                 res.data[i]['reason'] = "<button class='layui-btn purchase' gid='"+i+"'>确定</button>";
			}
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: [[
		  {field:'goods_id', title: '序号'}
		  ,{field:'goods_name', title: '产品名称'}
		  ,{field:'brand_name', title: '品牌'}
		  ,{field:'marker', title: '型号'}
		  ,{field:'memory_name', title: '内存'}
		  ,{field:'attribute_name', title: '属性'}
		  ,{field:'colour_name', title: '颜色'}
		  ,{field:'selling_price', title: '零售单价（元）'}
		  ,{field:'trade_price', title: '批发价（元）'}
          ,{field:'buying_price', title: '进货单价（元）'}
          ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
        ,done: function(res, curr, count){
             $('.purchase').on('click',function(){
               var id = $(this).attr('gid');
               var obj = res.data[id];

                  $("#gid", parent.document).val(obj.goods_id);
                  $("#gname", parent.document).val(obj.goods_name); 
               
               
               //关闭当前frame
               xadmin.close();
         })
        }
	  });
	}
	tableInit();
    
	//监听提交
	form.on('submit(sreach)',
	function(data) {
		tableInit(data.field);
		return false;
	});
    /*关闭页面*/
    form.on('submit(close)',
    function(data) {
        //关闭当前frame
        xadmin.close();
        return false;
    });
});

